<template>
	<div class="app-container">
		<el-form
			v-show="showSearch"
			ref="queryRef"
			:inline="true"
			:model="queryParams"
		>
			<el-form-item label="表名称" prop="tableName">
				<el-input
					v-model="queryParams.tableName"
					clearable
					placeholder="请输入表名称"
					style="width: 240px"
					@clear="handleQuery()"
					@keyup.enter="handleQuery()"
				/>
			</el-form-item>
			<el-form-item label="表描述" prop="tableComment">
				<el-input
					v-model="queryParams.tableComment"
					clearable
					placeholder="请输入表描述"
					style="width: 240px"
					@change="handleQuery()"
					@keyup.enter="handleQuery()"
				/>
			</el-form-item>
			<el-form-item
				label="创建时间"
				style="width: 400px; font-weight: bold"
			>
				<el-date-picker
					v-model="dateRange"
					end-placeholder="结束日期"
					format="YYYY-MM-DD"
					range-separator="-"
					start-placeholder="开始日期"
					type="daterange"
					value-format="YYYY-MM-DD"
					@change="handleQuery()"
				></el-date-picker>
			</el-form-item>
			<!-- <el-form-item class="item-search">
				<el-button icon="Refresh" @click="resetQuery()">重置</el-button>
				<el-button type="primary" icon="Search" @click="handleQuery()">搜索</el-button>
			</el-form-item> -->
			<form-search @reset="resetQuery" @search="handleQuery" />
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button
					v-hasPermi="['tool:gen:code']"
					:disabled="genCodeEnabled"
					icon="download"
					plain
					size="small"
					type="primary"
					@click="handleGenTable"
				>生成
				</el-button
				>
			</el-col>
			<el-col :span="1.5">
				<el-button
					v-hasPermi="['tool:gen:import']"
					icon="upload"
					plain
					size="small"
					type="info"
					@click="openImportTable"
				>导入
				</el-button
				>
			</el-col>
			<el-col v-if="!single" :span="1.5">
				<el-button
					v-hasPermi="['tool:gen:edit']"
					:disabled="single"
					icon="edit"
					plain
					size="small"
					type="success"
					@click="handleEditTable"
				>修改
				</el-button
				>
			</el-col>
			<el-col v-if="!multiple" :span="1.5">
				<el-button
					v-hasPermi="['tool:gen:remove']"
					:disabled="multiple"
					icon="delete"
					plain
					size="small"
					type="danger"
					@click="handleDelete"
				>删除
				</el-button
				>
			</el-col>
			<right-toolbar
				v-model:showSearch="showSearch"
				@queryTable="getPageList()"
			></right-toolbar>
		</el-row>

		<el-table
			ref="pageTableRef"
			v-loading="loading"
			:data="tableList"
			border
			stripe
			@selection-change="handleSelectionChange"
		>
			<el-table-column
				align="center"
				type="selection"
				width="55"
			></el-table-column>
			<el-table-column
				align="center"
				label="序号"
				type="index"
				width="100"
			>
				<template #default="scope">
					<!-- prettier-ignore -->
					<span>{{ ((queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1) }}</span>
				</template>
			</el-table-column>
			<el-table-column
				:show-overflow-tooltip="true"
				align="center"
				label="表名称"
				prop="tableName"
			/>
			<el-table-column
				:show-overflow-tooltip="true"
				align="center"
				label="表描述"
				prop="tableComment"
			/>
			<el-table-column
				:show-overflow-tooltip="true"
				align="center"
				label="实体"
				prop="className"
			/>
			<!-- dateTimeSub -->
			<el-table-column
				align="center"
				label="创建时间"
				prop="createTime"
				width="200"
			>
				<template #default="scope">
					{{ dateTimeSub(scope.row.createTime) }}
				</template>
			</el-table-column>
			<el-table-column
				align="center"
				label="更新时间"
				prop="updateTime"
				width="200"
			>
				<template #default="scope">
					{{ dateTimeSub(scope.row.updateTime) }}
				</template>
			</el-table-column>
			<el-table-column
				align="center"
				class-name="small-padding fixed-width"
				label="操作"
				width="400"
			>
				<template #default="scope">
					<el-link
						v-hasPermi="['tool:gen:preview']"
						:underline="false"
						class="table_link_btn"
						icon="View"
						type="primary"
						@click="handlePreview(scope.row)"
					><span class="table_link_text">预览</span></el-link
					>
					<el-link
						v-hasPermi="['tool:gen:edit']"
						:underline="false"
						class="table_link_btn"
						icon="Edit"
						type="primary"
						@click="handleEditTable(scope.row)"
					><span class="table_link_text">编辑</span></el-link
					>
					<el-link
						v-hasPermi="['tool:gen:code']"
						:underline="false"
						class="table_link_btn"
						icon="Download"
						type="primary"
						@click="handleGenTable(scope.row)"
					><span class="table_link_text">生成代码</span></el-link
					>
					<el-link
						v-hasPermi="['tool:gen:edit']"
						:underline="false"
						class="table_link_btn"
						icon="Refresh"
						type="warning"
						@click="handleSynchDb(scope.row)"
					><span class="table_link_text">同步</span></el-link
					>
					<el-link
						v-hasPermi="['tool:gen:remove']"
						:underline="false"
						class="table_link_btn"
						icon="Delete"
						type="danger"
						@click="handleDelete(scope.row)"
					><span class="table_link_text">删除</span></el-link
					>
				</template>
			</el-table-column>
		</el-table>
		<pagination
			v-show="total > 0"
			v-model:limit="queryParams.pageSize"
			v-model:page="queryParams.pageNum"
			:total="total"
			@pagination="getPageList()"
		/>
		<!-- 预览界面 -->
		<el-dialog
			v-model="preview.open"
			:title="preview.title"
			append-to-body
			custom-class="scrollbar"
			top="5vh"
			width="80%"
			@close="viewCodeClose()"
		>
			<el-tabs v-model="preview.activeName" type="border-card">
				<!-- prettier-ignore -->
				<el-tab-pane
					v-for="(value, key) in preview.data"
					:key="value"
					:label="key.toString().substring(key.toString().lastIndexOf('/') + 1,key.toString().indexOf('.vm'))"
					:name="key.toString().substring(key.toString().lastIndexOf('/') + 1,key.toString().indexOf('.vm'))"
				>
					<el-link
						v-copyText="value"
						v-copyText:callback="copyTextSuccess"
						:underline="false"
						icon="documentCopy"
						style="float: right;margin-top: 30px;"
					>复制
					</el-link
					>
					<highlightjs :code="value" />
				</el-tab-pane>
			</el-tabs>
		</el-dialog>
		<!-- prettier-ignore -->
		<import-table ref="importRef" @cleanTableSelect="cleanSelect()" @ok="handleQuery()" />
		<!-- 弹窗编辑模式 -->
		<div class="div_dialog">
			<el-dialog
				v-model="editVisable"
				custom-class="import-dialog"
				destroy-on-close
				title="编辑"
				width="100%"
				@close="cleanSelect()"
			>
				<!-- prettier-ignore -->
				<edit-table ref="editTableRef" :tableId="tableId" @cleanTableSelect="cleanSelect()"
							@ok="handleQuery()" />
				<template #footer>
					<!-- prettier-ignore -->
					<div class="dialog-footer" style="text-align: center;margin-top: -2vh;">
						<el-button @click="editVisable = false">取 消</el-button>
						<!-- prettier-ignore -->
						<el-button type="primary" @click="submitChildForm()">确 定</el-button>
					</div>
				</template>
			</el-dialog>
		</div>
	</div>
</template>

<script lang="ts" name="Gen" setup>
import importTable from "./importTable.vue";
import editTable from "./editTable.vue";
import Gen from "@/api/request/system/tool/gen";
// prettier-ignore
const {
	loading,
	queryRef,
	pageTableRef,
	showSearch,
	genCodeEnabled,
	single,
	multiple,
	total,
	tableList,
	dateRange,
	queryParams,
	preview,
	getPageList,
	handleQuery,
	resetQuery,
	openImportTable,
	copyTextSuccess,
	handlePreview,
	handleSelectionChange,
	handleDelete,
	handleEditTable,
	editTableRef,
	handleGenTable,
	handleSynchDb,
	changeStatus,
	viewCodeClose,
	cleanSelect,
	editVisable,
	tableId,
	submitChildForm,
	switchEdit,
	editTypeTitle,
} = Gen();
</script>
<style lang="scss" scoped>
pre {
	margin: -28px 0 0 0 !important;
	font-size: 1.2em !important;
	/* prettier-ignore */
	font-family: "Consolas", "Bitstream Vera Sans Mono", "Courier New", Courier, "Poppins", sans-serif, monospace !important;
}

.div_dialog {
	//弹出层的高度
	::deep .el-dialog.import-dialog {
		height: auto;
		max-height: 50vh;
		overflow-y: auto;
	}

	//弹出层里内容的高度
	::deep .el-dialog__body {
		max-height: 60vh !important;
	}
}

</style>
